package com.javaweb.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.KDLMaterial;
import com.javaweb.admin.entity.KDLMaterialLog;
import com.javaweb.admin.entity.KDLSupplier;
import com.javaweb.admin.entity.KDLSupplierLog;
import com.javaweb.admin.mapper.KDLMaterialMapper;
import com.javaweb.admin.query.KDLMaterialQuery;
import com.javaweb.admin.query.KDLSupplierQuery;
import com.javaweb.admin.service.IKDLMaterialService;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class KDLMaterialServiceImpl extends BaseServiceImpl<KDLMaterialMapper, KDLMaterial> implements IKDLMaterialService {

    @Autowired
    private KDLMaterialMapper kdlMaterialMapper;

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        KDLMaterialQuery customerQuery = (KDLMaterialQuery) query;
        // 查询条件
        QueryWrapper<KDLMaterial> queryWrapper = new QueryWrapper<>();
        // 会员等级名称
        if (!StringUtils.isEmpty(customerQuery.getMaterialcode())) {
            queryWrapper.and(wq->{ wq.like("materialcode", customerQuery.getMaterialcode()).or().like("materialdes",customerQuery.getMaterialcode()).or().like("comdescription",customerQuery.getMaterialcode());});
        }
        if(!StringUtils.isEmpty(customerQuery.getMaterialgroup())){
            queryWrapper.like("materialgroup",customerQuery.getMaterialgroup());
        }
        if(!StringUtils.isEmpty(customerQuery.getLregistno())){
            queryWrapper.like("lregistno",customerQuery.getLregistno());
        }
        if(!StringUtils.isEmpty(customerQuery.getManufacturername())){
            queryWrapper.like("manufacturername",customerQuery.getManufacturername());
        }
        if (!StringUtils.isEmpty(customerQuery.getRiqiBegin())) {
            queryWrapper.gt("expirationdate", customerQuery.getRiqiBegin());
        }
        if (!StringUtils.isEmpty(customerQuery.getRiqiEnd())) {
            queryWrapper.lt("expirationdate", customerQuery.getRiqiEnd());
        }

        if (!StringUtils.isEmpty(customerQuery.getStatus())) {
            queryWrapper.eq("status", customerQuery.getStatus());
        }
        if (!StringUtils.isEmpty(customerQuery.getFcbz())) {
            queryWrapper.eq("fcbz", customerQuery.getFcbz());
        }

        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("ncts");

        // 查询分页数据
        IPage<KDLMaterial> page = new Page<>(customerQuery.getPage(), customerQuery.getLimit());
        IPage<KDLMaterial> pageData = kdlMaterialMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getMaterialList(BaseQuery query) {
        KDLMaterialQuery materialQuery = (KDLMaterialQuery) query;
        // 查询分页数据
        IPage<KDLMaterial> page = new Page<>(materialQuery.getPage(), materialQuery.getLimit());
        IPage<KDLMaterial> pageData = kdlMaterialMapper.getMaterialList(page, materialQuery);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public List<KDLMaterialLog> getNCMaterialLogList(String materialcode) {
        return kdlMaterialMapper.GetNCMaterialLogList(materialcode);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getMaterialGroupList() {
        return JsonResult.success(kdlMaterialMapper.getMaterialGroup());
    }
}
